cd "C:\Users\David\Dropbox\DDM - Criminal Justice\POQ\Replication Archive"
//SET WORKING DIRECTORY. DIRECTORY SHOULD INCLUDE SUBFOLDERS NAMED "tables" AND "graphs"
set mat 2000
use "cces_2020.dta", clear
//SET DIRECTORY TO SAVE GRAPH/TABLE OUTPUT
cd "tables"

*****************
***DEMOGRAPHICS
*****************
gen age=2020-birthyr
label var age "Respondent's Age (in years)"
gen female = gender-1
label var female "Female (1=yes)"
gen income=faminc_new
sum income if faminc_new!=97
replace income=r(mean) if faminc_new==97
gen income_mis=faminc_new==97
label var income "Family Income (1-16)"
label var income_mis "Family Income Refused (1=yes)"
label var educ "Education (1-6)"
recode race (1=1) (2=2) (3=3) (*=4), gen(race4)
tab race4, gen(race_)
label var race_1 "White (1=yes)"
label var race_2 "Black (1=yes)"
label var race_3 "Hispanic (1=yes)"
label var race_4 "Other Race/Ethnicity (1=yes)"
recode pid7 (1/3=1) (4 8=2) (5/7=3), gen(newpid)
tab newpid, gen(pid_)
label var pid_1 "Democrat (1=yes)"
label var pid_2 "Independent/Not Sure (1=yes)"
label var pid_3 "Republican (1=yes)"

recode pid7 (8=4), gen(pid)
label var pid "Party Identification (1=Strong D; 7=Strong R)"


*********CONJOINT VARIABLES ***********

//RENAME VARIABLES
local c=1
forvalues i=461/466{
gen sentence`c'=UMS`i'
gen months`c'=UMS`i'_mos
gen years`c'=UMS`i'_yrs
gen conj_age`c'=UMS`i'_age
gen name`c'=UMS`i'_name
gen crime`c'=UMS`i'_crime
gen priors`c'=UMS`i'_priors
gen probation`c'=UMS`i'_prob
gen job`c'=UMS`i'_job
gen life`c'=sentence`c'==4
local c=`c'+1
}
//Number of tasks with a response
egen tasks_completed=rownonmiss(sentence*)
//drop if no conjoint responses
drop if tasks_completed==0

//Cognitive Racial Attitudes
alpha CC20_440a CC20_440b CC20_441a CC20_441b, i gen(race_att)
alpha CC20_440a CC20_440b, gen(cobra_race)
alpha CC20_441a CC20_441b, gen(rr_race)
foreach i in race_att cobra_race rr_race{
qui sum `i'
replace `i'=(`i'-r(mean))/r(sd)
}
replace rr_race=rr_race*-1
replace race_att=race_att*-1
label var race_att "Cognitive Racial Attitudes (M=0; SD=1)"
label var rr_race "Cognitive Racial Attitudes (RR Only; M=0; SD=1)"
label var cobra_race "Cognitive Racial Attitudes (CoBRAS Only; M=0; SD=1)"

**SUMMARY STATS
estpost summarize age female educ income income_mis race_1 race_2 race_3 race_4 pid_1 pid_2 pid_3 pid
eststo sum_stats, title("Summary Statistics")
esttab sum_stats using "Table_A1.tex", cells(mean(fmt(2))) replace nogaps booktabs wrap varwidth(40) b(%9.3f) se(%9.3f) star(+ 0.10 * 0.05 ** .01) nonotes nodepvars label  fragment

//RESHAPE TO STACK TASKS
reshape long sentence months years conj_age name crime priors probation job, i(caseid) j(task)
keep sentence months years conj_age name crime priors probation job caseid pid race_att rr_race cobra_race CC20_440a CC20_440b CC20_441a CC20_441b age female educ income income_mis race_1 race_2 race_3 race_4 pid_1 pid_2 pid_3 pid 

//OUTCOME MEASURE
gen sentence_scaled=0 if sentence==1
replace sentence_scaled=months if sentence==2
replace sentence_scaled=years*12 if sentence==3
replace sentence_scaled=99*12 if sentence==4
label var sentence_scaled "Sentence (in months)"
drop if sentence_scaled==.
//TREATMENTS
//Crime
tab crime, gen(crime_)
label var crime_1 "Gun: Firing/Missing"
label var crime_2 "Forgery: $60,000"
label var crime_3 "Forgery: $600,000"
label var crime_4 "Forgery: $5,000"
label var crime_5 "Drugs: 1/3oz heroin"
label var crime_6 "Drugs: 6.8lbs heroin"
label var crime_7 "Gun: Shooting"
label var crime_8 "Gun: Threaten to pull"
//Priors
tab priors, gen(priors_)
//fix weirdness in ordering if we go in alphabetical order
rename priors_2 priors_X
rename priors_3 priors_2
rename priors_X priors_3
label var priors_1 "Priors: None"
label var priors_2 "Priors: 6 month"
label var priors_3 "Priors: 2 year"
//Probation
tab probation, gen(probation_)
label var probation_1 "On Probation: No"
label var probation_2 "On Probation: Yes"
//Age
recode conj_age (15/17=1) (18/21=2) (22/30=3), gen(def_age)
tab def_age, gen(def_age_)
label var def_age_1 "Age: 15-17"
label var def_age_2 "Age: 18-21"
label var def_age_3 "Age: 22-30"
//Name
gen black=0
replace black =1 if name=="DeShawn Jackson" |name=="Tyrone Brown" |name=="Darius Thomas" |name=="Jamal Jones" |name=="Terrell Turner" |name=="Trevon Williams" |name=="DeAndre Wilkins" |name=="Darnell Haynes" |name=="Jalen Washington" |name=="Marquis Harris" |name=="Malik Johnson"
gen white=abs(black-1)
label var black "Black Name"
label var white "White Name"
//Job
tab job, gen(job_)
label var job_1 "Employed: No"
label var job_2 "Employed: Yes"

****number of profiles (for Reviewer)
preserve
egen g_crime=group(crime)
egen g_priors=group(priors)
egen g_probation=group(probation)
egen g_def_age=group(def_age)
egen g_job=group(job)

gen raw_profile=g_crime*10+g_priors*100+g_probation*1000+g_def_age*10000+g_job*100000+black
egen profile=group(raw_profile)
collapse (count) black, by(profile)
histogram black, width(1) start(0) frequency xtitle("Number of Times Profile Presented")  graphregion(fcolor(white) lcolor(none) ilcolor(none) color(white) lwidth(large))
graph export "profiles.png", as(png) replace
restore

//CHOOSE REFERENCE GROUPS
gen REF_race = 0
label var REF_race "White Name"
gen REF_crime = 0
label var REF_crime "Forgery: $5,000"
gen REF_priors = 0
label var REF_priors "Priors: None"
gen REF_probation = 0
label var REF_probation "On Probation: No"
gen REF_age = 0
label var REF_age "Age: 15-17"
gen REF_job = 0
label var REF_job "Employed: No"

***DIRECT EFFECTS
reg sentence_scaled REF_race black REF_crime crime_2 crime_3 crime_8 crime_1 crime_7 crime_5 crime_6 REF_priors priors_2 priors_3 REF_probation probation_2 REF_age def_age_2 def_age_3 REF_job job_2, cluster(caseid)
eststo direct_FX_noctrl, title("Prescribed Sentence in Months")
reg sentence_scaled REF_race black REF_crime crime_2 crime_3 crime_8 crime_1 crime_7 crime_5 crime_6 REF_priors priors_2 priors_3 REF_probation probation_2 REF_age def_age_2 def_age_3 REF_job job_2 age female educ income income_mis race_2 race_3 race_4 pid_2 pid_3, cluster(caseid)
eststo direct_FX, title("Prescribed Sentence in Months")
coefplot (direct_FX, msymbol(Oh) connect(none) mcolor(black)  msize(vsmall) ciopts(lcolor(black) lwidth(thin) msize(vtiny))) , omitted xline(0, lcolor(black)) level(95) xtitle("{bf:Estimated Effect on Sentence (in months)}",  size(vsmall))  graphregion(fcolor(white) lcolor(none) ilcolor(none) color(white) lwidth(large)) xlabel(, labsize(vsmall)) xscale(lw(none))  legend(off) coeflabels(,wrap(40)) keep( REF_race black REF_crime crime_2 crime_3 crime_5 crime_6 crime_8 crime_1 crime_7 REF_priors priors_2 priors_3 REF_probation probation_2 REF_age def_age_2 def_age_3 REF_job job_2 ) ylabel(,labsize(vsmall)) headings(REF_race = "{bf:Defendant Name}" REF_crime = "{bf:Crime}" REF_priors = "{bf:Priors?}" REF_probation = "{bf:On Probation?}" REF_age = "{bf:Defendant Age}" REF_job = "{bf:Employment Status}", labsize(vsmall))
graph export "Figure_1.eps", as(eps) replace

*** INTERACTIONS (DEF RACE x OTHER TREAT + ALL TREATS x ALL RESP CHARS)
*Sensible ref for continuous demographics (50yo; HS grad; income $50-59k)
replace age=age-50
replace educ=educ-2
replace income = income-6

foreach i of varlist black crime_2 crime_3 crime_8 crime_1 crime_7 crime_5 crime_6 priors_2 priors_3 REF_probation probation_2 def_age_2 def_age_3 job_2 {
foreach v of varlist black white pid_1 pid_2 pid_3 race_1 race_2 race_3 race_4 age female educ income income_mis {
gen X`v'X`i'=`v'*`i'
}
local label : variable label `i'
label var XblackX`i' "Black Name x `label'"
label var XwhiteX`i' "White Name x `label'"
label var Xpid_1X`i' "Democrat x `label'"
label var Xpid_2X`i' "Independent x `label'"
label var Xpid_3X`i' "Republican x `label'"
label var Xrace_1X`i' "White R x `label'"
label var Xrace_2X`i' "Black R x `label'"
label var Xrace_3X`i' "Hispanic R x `label'"
label var Xrace_4X`i' "Other Race/Eth. R x `label'"
label var XageX`i' "R's Age x `label'"
label var XfemaleX`i' "Female x `label'"
label var XeducX`i' "Education x `label'"
label var XincomeX`i' "Income x `label'"
label var Xincome_misX`i' "Missing Income x `label'"
}
drop XblackXblack XwhiteXblack 


**RACE OF DEFENDANT INTERACTIONS
reg sentence_scaled XblackX* REF_race black REF_crime crime_2 crime_3 crime_8 crime_1 crime_7 crime_5 crime_6 REF_priors priors_2 priors_3 REF_probation probation_2 REF_age def_age_2 def_age_3 REF_job job_2, cluster(caseid)
eststo whiteD_FX_noctrl, title("Prescribed Sentence in Months")
reg sentence_scaled XblackX* REF_race black REF_crime crime_2 crime_3 crime_8 crime_1 crime_7 crime_5 crime_6 REF_priors priors_2 priors_3 REF_probation probation_2 REF_age def_age_2 def_age_3 REF_job job_2 age female educ income income_mis race_2 race_3 race_4 pid_2 pid_3, cluster(caseid)
test XblackXcrime_2 XblackXcrime_3 XblackXcrime_8 XblackXcrime_1 XblackXcrime_7 XblackXcrime_5 XblackXcrime_6 XblackXpriors_2 XblackXpriors_3 XblackXprobation_2 XblackXdef_age_2 XblackXdef_age_3 XblackXjob_2
eststo whiteD_FX, title("Prescribed Sentence in Months")

reg sentence_scaled XwhiteX* REF_race black REF_crime crime_2 crime_3 crime_8 crime_1 crime_7 crime_5 crime_6 REF_priors priors_2 priors_3 REF_probation probation_2 REF_age def_age_2 def_age_3 REF_job job_2 age female educ income income_mis race_2 race_3 race_4 pid_2 pid_3, cluster(caseid)
eststo blackD_FX
coefplot (whiteD_FX, msymbol(O) connect(none) mcolor(gs10)  msize(vsmall) ciopts(lcolor(gs10) lwidth(thin) msize(vtiny)) label(White Name)) (blackD_FX, msymbol(Oh) connect(none) mcolor(black)  msize(vsmall) ciopts(lcolor(black) lwidth(thin) msize(vtiny)) label(Black Name)) , omitted xline(0, lcolor(black)) level(95) xtitle("{bf:Estimated Effect on Sentence (in months)}",  size(vsmall))  graphregion(fcolor(white) lcolor(none) ilcolor(none) color(white) lwidth(large)) xlabel(, labsize(vsmall)) xscale(lw(none))  legend(off) coeflabels(,wrap(40)) keep(REF_crime crime_2 crime_3 crime_5 crime_6 crime_8 crime_1 crime_7 REF_priors priors_2 priors_3 REF_probation probation_2 REF_age def_age_2 def_age_3 REF_job job_2 ) ylabel(,labsize(vsmall)) headings(REF_crime = "{bf:Crime}" REF_priors = "{bf:Priors?}" REF_probation = "{bf:On Probation?}" REF_age = "{bf:Defendant Age}" REF_job = "{bf:Employment Status}", labsize(vsmall)) text(2.5 25 "White Name", place(c) size(vsmall) color(gs8)) text(2.75 70 "Black Name", place(c) size(vsmall) color(black))
graph export "Figure_3.eps", as(eps) replace

**RESPONDENT CHARACTERISTIC INTERACTIONS
//By Respondent PID
reg sentence_scaled REF_race black REF_crime crime_2 crime_3 crime_8 crime_1 crime_7 crime_5 crime_6 REF_priors priors_2 priors_3 REF_probation probation_2 REF_age def_age_2 def_age_3 REF_job job_2 age female educ income income_mis race_2 race_3 race_4 pid_2 pid_3 Xpid_2X* Xpid_3X* Xrace_2X* Xrace_3X* Xrace_4X* XageX* XfemaleX* XeducX* XincomeX* Xincome_misX*, cluster(caseid)
test Xpid_3Xblack Xpid_3Xcrime_2 Xpid_3Xcrime_3 Xpid_3Xcrime_8 Xpid_3Xcrime_1 Xpid_3Xcrime_7 Xpid_3Xcrime_5 Xpid_3Xcrime_6 Xpid_3Xpriors_2 Xpid_3Xpriors_3 Xpid_3Xprobation_2 Xpid_3Xdef_age_2 Xpid_3Xdef_age_3 Xpid_3Xjob_2
eststo demR_FX, title("Prescribed Sentence in Months")
reg sentence_scaled REF_race black REF_crime crime_2 crime_3 crime_8 crime_1 crime_7 crime_5 crime_6 REF_priors priors_2 priors_3 REF_probation probation_2 REF_age def_age_2 def_age_3 REF_job job_2 age female educ income income_mis race_2 race_3 race_4 pid_2 pid_3 Xpid_2X* Xpid_3X*, cluster(caseid)
test Xpid_3Xblack Xpid_3Xcrime_2 Xpid_3Xcrime_3 Xpid_3Xcrime_8 Xpid_3Xcrime_1 Xpid_3Xcrime_7 Xpid_3Xcrime_5 Xpid_3Xcrime_6 Xpid_3Xpriors_2 Xpid_3Xpriors_3 Xpid_3Xprobation_2 Xpid_3Xdef_age_2 Xpid_3Xdef_age_3 Xpid_3Xjob_2
eststo demR_FX_pidonly, title("Party ID Interactions Only")

reg sentence_scaled Xpid_1X* Xpid_3X* Xrace_2X* REF_race black REF_crime crime_2 crime_3 crime_8 crime_1 crime_7 crime_5 crime_6 REF_priors priors_2 priors_3 REF_probation probation_2 REF_age def_age_2 def_age_3 REF_job job_2 age female educ income income_mis race_2 race_3 race_4 pid_2 pid_3  Xrace_3X* Xrace_4X* XageX* XfemaleX* XeducX* XincomeX* Xincome_misX*, cluster(caseid)
eststo indR_FX
reg sentence_scaled Xpid_1X* Xpid_2X* Xrace_2X* REF_race black REF_crime crime_2 crime_3 crime_8 crime_1 crime_7 crime_5 crime_6 REF_priors priors_2 priors_3 REF_probation probation_2 REF_age def_age_2 def_age_3 REF_job job_2 age female educ income income_mis race_2 race_3 race_4 pid_2 pid_3  Xrace_3X* Xrace_4X* XageX* XfemaleX* XeducX* XincomeX* Xincome_misX*, cluster(caseid)
eststo repR_FX
coefplot (demR_FX, msymbol(O) connect(none) mcolor(gs10)  msize(vsmall) ciopts(lcolor(gs10) lwidth(thin) msize(vtiny)) label(Democratic Respondent)) (repR_FX, msymbol(Oh) connect(none) mcolor(black)  msize(vsmall) ciopts(lcolor(black) lwidth(thin) msize(vtiny)) label(Republican Respodent)) , omitted xline(0, lcolor(black)) level(95) xtitle("{bf:Estimated Effect on Sentence (in months)}",  size(vsmall))  graphregion(fcolor(white) lcolor(none) ilcolor(none) color(white) lwidth(large)) xlabel(, labsize(vsmall)) xscale(lw(none))  legend(off) coeflabels(,wrap(40)) keep(REF_race black REF_crime crime_2 crime_3 crime_5 crime_6 crime_8 crime_1 crime_7 REF_priors priors_2 priors_3 REF_probation probation_2 REF_age def_age_2 def_age_3 REF_job job_2 ) ylabel(,labsize(vsmall)) headings(REF_race = "{bf:Defendant Name}" REF_crime = "{bf:Crime}" REF_priors = "{bf:Priors?}" REF_probation = "{bf:On Probation?}" REF_age = "{bf:Defendant Age}" REF_job = "{bf:Employment Status}", labsize(vsmall)) text(2.6 85 "Democratic Respondent", place(c) size(vsmall) color(gs8)) text(3.3 90 "Republican Respondent", place(c) size(vsmall) color(black))
graph export "Figure_A3.eps", as(eps) replace

//By Respondent Race
reg sentence_scaled Xpid_1X* Xpid_3X* Xrace_2X* REF_race black REF_crime crime_2 crime_3 crime_8 crime_1 crime_7 crime_5 crime_6 REF_priors priors_2 priors_3 REF_probation probation_2 REF_age def_age_2 def_age_3 REF_job job_2 age female educ income income_mis race_2 race_3 race_4 pid_2 pid_3  Xrace_3X* Xrace_4X* XageX* XfemaleX* XeducX* XincomeX* Xincome_misX*, cluster(caseid)
test Xrace_2Xblack Xrace_2Xcrime_2 Xrace_2Xcrime_3 Xrace_2Xcrime_8 Xrace_2Xcrime_1 Xrace_2Xcrime_7 Xrace_2Xcrime_5 Xrace_2Xcrime_6 Xrace_2Xpriors_2 Xrace_2Xpriors_3 Xrace_2Xprobation_2 Xrace_2Xdef_age_2 Xrace_2Xdef_age_3 Xrace_2Xjob_2
eststo whiteR_FX
reg sentence_scaled REF_race black REF_crime crime_2 crime_3 crime_8 crime_1 crime_7 crime_5 crime_6 REF_priors priors_2 priors_3 REF_probation probation_2 REF_age def_age_2 def_age_3 REF_job job_2 age female educ income income_mis race_2 race_3 race_4 pid_2 pid_3 Xrace_2X* Xrace_3X* Xrace_4X*, cluster(caseid)
test Xrace_2Xblack Xrace_2Xcrime_2 Xrace_2Xcrime_3 Xrace_2Xcrime_8 Xrace_2Xcrime_1 Xrace_2Xcrime_7 Xrace_2Xcrime_5 Xrace_2Xcrime_6 Xrace_2Xpriors_2 Xrace_2Xpriors_3 Xrace_2Xprobation_2 Xrace_2Xdef_age_2 Xrace_2Xdef_age_3 Xrace_2Xjob_2
eststo blackR_FX_raceonly, title("Respondent Race Interactions Only")

reg sentence_scaled Xpid_1X* Xpid_3X* Xrace_1X* REF_race black REF_crime crime_2 crime_3 crime_8 crime_1 crime_7 crime_5 crime_6 REF_priors priors_2 priors_3 REF_probation probation_2 REF_age def_age_2 def_age_3 REF_job job_2 age female educ income income_mis race_2 race_3 race_4 pid_2 pid_3  Xrace_3X* Xrace_4X* XageX* XfemaleX* XeducX* XincomeX* Xincome_misX*, cluster(caseid)
eststo blackR_FX
coefplot (whiteR_FX, msymbol(O) connect(none) mcolor(gs10)  msize(vsmall) ciopts(lcolor(gs10) lwidth(thin) msize(vtiny)) label(White Respondent)) (blackR_FX, msymbol(Oh) connect(none) mcolor(black)  msize(vsmall) ciopts(lcolor(black) lwidth(thin) msize(vtiny)) label(Black Respodent)) , omitted xline(0, lcolor(black)) level(95) xtitle("{bf:Estimated Effect on Sentence (in months)}",  size(vsmall))  graphregion(fcolor(white) lcolor(none) ilcolor(none) color(white) lwidth(large)) xlabel(, labsize(vsmall)) xscale(lw(none))  legend(off) coeflabels(,wrap(40)) keep(REF_race black REF_crime crime_2 crime_3 crime_5 crime_6 crime_8 crime_1 crime_7 REF_priors priors_2 priors_3 REF_probation probation_2 REF_age def_age_2 def_age_3 REF_job job_2 ) ylabel(,labsize(vsmall)) headings(REF_race = "{bf:Defendant Name}" REF_crime = "{bf:Crime}" REF_priors = "{bf:Priors?}" REF_probation = "{bf:On Probation?}" REF_age = "{bf:Defendant Age}" REF_job = "{bf:Employment Status}", labsize(vsmall)) text(2.6 108 "White Respondent", place(c) size(vsmall) color(gs8)) text(3.3 85 "Black Respondent", place(c) size(vsmall) color(black))
graph export "Figure_A2.eps", as(eps) replace

**HANDLE SPECIAL CHARACTERS FOR TEX OUTPUT (forgery $s)
foreach v of varlist *crime_2* *crime_3*{
local label : variable label `v'
local newlab=subinstr("`label'","$","\\\$",.)
label var `v' "`newlab'"
}
***LOGGED OUTCOME
gen ln_sentence=ln(sentence_scaled+1)
label var ln_sentence "Sentence (in months; logged)"
//CORE ANALYSIS
reg ln_sentence REF_race black REF_crime crime_2 crime_3 crime_8 crime_1 crime_7 crime_5 crime_6 REF_priors priors_2 priors_3 REF_probation probation_2 REF_age def_age_2 def_age_3 REF_job job_2 age female educ income income_mis race_2 race_3 race_4 pid_2 pid_3, cluster(caseid)
eststo ln_direct_FX, title("Sentence in Months (Logged)")
reg ln_sentence XblackX* REF_race black REF_crime crime_2 crime_3 crime_8 crime_1 crime_7 crime_5 crime_6 REF_priors priors_2 priors_3 REF_probation probation_2 REF_age def_age_2 def_age_3 REF_job job_2 age female educ income income_mis race_2 race_3 race_4 pid_2 pid_3, cluster(caseid)
eststo ln_whiteD_FX, title("Sentence in Months (Logged)")

gen capped_sentence=sentence_scaled
replace capped_sentence=300 if capped_sentence>300 & capped_sentence!=.
label var capped_sentence "Sentence (in months; top-coded at 300)"

gen chopped_sentence=sentence_scaled
replace chopped_sentence=. if sentence_scaled>300 
label var capped_sentence "Sentence (in months; exclude if > 300)"

qui reg capped_sentence REF_race black REF_crime crime_2 crime_3 crime_8 crime_1 crime_7 crime_5 crime_6 REF_priors priors_2 priors_3 REF_probation probation_2 REF_age def_age_2 def_age_3 REF_job job_2 age female educ income income_mis race_2 race_3 race_4 pid_2 pid_3, cluster(caseid)
eststo cap_direct_FX, title("Sentence in Months (Top-Coded)")
qui reg capped_sentence XblackX* REF_race black REF_crime crime_2 crime_3 crime_8 crime_1 crime_7 crime_5 crime_6 REF_priors priors_2 priors_3 REF_probation probation_2 REF_age def_age_2 def_age_3 REF_job job_2 age female educ income income_mis race_2 race_3 race_4 pid_2 pid_3, cluster(caseid)
eststo cap_whiteD_FX, title("Sentence in Months (Top-Coded)")

qui reg chopped_sentence REF_race black REF_crime crime_2 crime_3 crime_8 crime_1 crime_7 crime_5 crime_6 REF_priors priors_2 priors_3 REF_probation probation_2 REF_age def_age_2 def_age_3 REF_job job_2 age female educ income income_mis race_2 race_3 race_4 pid_2 pid_3, cluster(caseid)
eststo chop_direct_FX, title("Sentence in Months (Exclude if > 300)")
qui reg chopped_sentence XblackX* REF_race black REF_crime crime_2 crime_3 crime_8 crime_1 crime_7 crime_5 crime_6 REF_priors priors_2 priors_3 REF_probation probation_2 REF_age def_age_2 def_age_3 REF_job job_2 age female educ income income_mis race_2 race_3 race_4 pid_2 pid_3, cluster(caseid)
eststo chop_whiteD_FX, title("Sentence in Months (Exclude if > 300)")


//SUBGROUP ANALYSIS
reg ln_sentence REF_race black REF_crime crime_2 crime_3 crime_8 crime_1 crime_7 crime_5 crime_6 REF_priors priors_2 priors_3 REF_probation probation_2 REF_age def_age_2 def_age_3 REF_job job_2 age female educ income income_mis race_2 race_3 race_4 pid_2 pid_3 Xpid_2X* Xpid_3X* Xrace_2X* Xrace_3X* Xrace_4X* XageX* XfemaleX* XeducX* XincomeX* Xincome_misX*, cluster(caseid)
eststo ln_demR_FX, title("Sentence in Months (Logged)")
reg ln_sentence REF_race black REF_crime crime_2 crime_3 crime_8 crime_1 crime_7 crime_5 crime_6 REF_priors priors_2 priors_3 REF_probation probation_2 REF_age def_age_2 def_age_3 REF_job job_2 age female educ income income_mis race_2 race_3 race_4 pid_2 pid_3 Xrace_2X* Xrace_3X* Xrace_4X*, cluster(caseid)
eststo ln_blackR_FX_raceonly, title("Sentence in Months (Logged)")
reg ln_sentence REF_race black REF_crime crime_2 crime_3 crime_8 crime_1 crime_7 crime_5 crime_6 REF_priors priors_2 priors_3 REF_probation probation_2 REF_age def_age_2 def_age_3 REF_job job_2 age female educ income income_mis race_2 race_3 race_4 pid_2 pid_3 Xpid_2X* Xpid_3X*, cluster(caseid)
eststo ln_demR_FX_pidonly, title("Party ID Interactions Only")


***OUTPUT REGRESSIONS
//CORE MODELS + NO CONTROLS
esttab direct_FX whiteD_FX direct_FX_noctrl whiteD_FX_noctrl  using Table_A3.tex, drop(*REF*) replace nogaps booktabs wrap varwidth(40) cells("b(fmt(3))" se(par(`"("' `")"') fmt(3)) p(par(`"["' `"]"') fmt(3))) nostar longtable nonotes addnotes("Cell entries are OLS coefficients; standard errors in parentheses; p-values in brackets.")  label nodepvars fragment
//LOGGED, OUTLIERS
esttab ln_direct_FX ln_whiteD_FX cap_direct_FX cap_whiteD_FX chop_direct_FX chop_whiteD_FX using Table_A5.tex, drop(*REF*) replace nogaps booktabs wrap varwidth(40) cells("b(fmt(3))" se(par(`"("' `")"') fmt(3)) p(par(`"["' `"]"') fmt(3))) nostar longtable nonotes addnotes("Cell entries are OLS coefficients; standard errors in parentheses; p-values in brackets.")  label nodepvars fragment

//RESP RACE/PID INTERACTIONS
esttab demR_FX blackR_FX_raceonly demR_FX_pidonly ln_demR_FX ln_blackR_FX_raceonly ln_demR_FX_pidonly using Table_A6.tex, drop(*REF* XageX* XfemaleX* XeducX* XincomeX* Xincome_misX*) replace nogaps booktabs wrap varwidth(40) cells("b(fmt(3))" se(par(`"("' `")"') fmt(3)) p(par(`"["' `"]"') fmt(3))) nostar longtable nonotes addnotes("Cell entries are OLS coefficients; standard errors in parentheses; p-values in brackets.")  label nodepvars fragment

//RACIAL ATTITUDE INTERACTIONS
qui reg sentence_scaled (REF_race black REF_crime crime_2 crime_3 crime_8 crime_1 crime_7 crime_5 crime_6 REF_priors priors_2 priors_3 REF_probation probation_2 REF_age def_age_2 def_age_3 REF_job job_2)##c.race_att age female educ income income_mis race_2 race_3 race_4 pid_2 pid_3, cluster(caseid)
eststo race_att, title("All Respondents")
qui reg sentence_scaled (black crime_2 crime_3 crime_8 crime_1 crime_7 crime_5 crime_6 priors_2 priors_3 probation_2 def_age_2 def_age_3 job_2)##(c.race_att c.age female c.educ c.income income_mis race_2 race_3 race_4 pid_2 pid_3), cluster(caseid)
eststo race_att_ctrls, title("All Respondents")
qui reg sentence_scaled (black crime_2 crime_3 crime_8 crime_1 crime_7 crime_5 crime_6 priors_2 priors_3 probation_2 def_age_2 def_age_3 job_2)##(c.race_att c.age female c.educ c.income income_mis pid_2 pid_3) if race_1==1, cluster(caseid)
eststo race_att_white, title("White Only")
//Swap in measures using subsets of racial attitude questions
gen HOLD_race_att=race_att
replace race_att=cobra_race
qui reg sentence_scaled (black crime_2 crime_3 crime_8 crime_1 crime_7 crime_5 crime_6 priors_2 priors_3 probation_2 def_age_2 def_age_3 job_2)##(c.race_att c.age female c.educ c.income income_mis race_2 race_3 race_4 pid_2 pid_3), cluster(caseid)
eststo race_att_cobras, title("CoBRAS")
replace race_att=rr_race
qui reg sentence_scaled (black crime_2 crime_3 crime_8 crime_1 crime_7 crime_5 crime_6 priors_2 priors_3 probation_2 def_age_2 def_age_3 job_2)##(c.race_att c.age female c.educ c.income income_mis race_2 race_3 race_4 pid_2 pid_3), cluster(caseid)
eststo race_att_rr, title("Racial Resentment")
esttab race_att race_att_ctrls race_att_white race_att_cobras race_att_rr using Table_A4.tex, keep(1.black race_att 1.black#c.race_att) replace nogaps booktabs wrap varwidth(40) cells("b(fmt(3))" se(par(`"("' `")"') fmt(3)) p(par(`"["' `"]"') fmt(3))) nostar longtable nonotes addnotes("Cell entries are OLS coefficients; standard errors in parentheses; p-values in brackets.")  label nodepvars fragment
replace race_att=HOLD_race_att
drop HOLD_race_att

****FIGURE 2
reg sentence_scaled (black crime_2 crime_3 crime_8 crime_1 crime_7 crime_5 crime_6 priors_2 priors_3 probation_2 def_age_2 def_age_3 job_2)##(c.race_att c.age female c.educ c.income income_mis race_2 race_3 race_4 pid_2 pid_3) , cluster(caseid)
margins black, atmeans at(race_att=(-2.4(.1)1.3)) dydx(black)
marginsplot, xscale(range(-2.6 1.5)) xlabel(-2.4 "Lowest"  1.3 "Highest") recast(line) recastci(rline) plot1opts(lcolor(black) lpattern(solid) lwidth(thick)) ci1opts(lcolor(gs8) lwidth(thin)) graphregion(fcolor(white) lcolor(none) ilcolor(none) color(white) lwidth(large)) xtitle("{bf:Cognitive Racial Attitudes}" "(Awareness of Racism; Continuous)") ytitle(`"{bf:Effect of {it:Black Name} Treatment}"') ylab(-48(12)96, nogrid) title("", size(vlarge)) legend(off) level(95) scheme(s1mono) yline(0)
graph save Graph "..\graphs\by_racial_atts_cont.gph", replace
//By Quintiles
xtile Q_race = race_att if race_att<1.3, nq(4)
replace Q_race = 5 if race_att>1.3
replace Q_race =Q_race -1
reg sentence_scaled (black crime_2 crime_3 crime_8 crime_1 crime_7 crime_5 crime_6 priors_2 priors_3 probation_2 def_age_2 def_age_3 job_2)##(i.Q_race c.age female c.educ c.income income_mis race_2 race_3 race_4 pid_2 pid_3) , cluster(caseid)
margins black, atmeans at(Q_race=(0(1)4)) dydx(black)
marginsplot, xscale(range(-.3 4.3)) xlabel(0 "Lowest"  4 "Highest") plotopts(lcolor(none) mcolor(black) ) ciopts(lcolor(black)) graphregion(fcolor(white) lcolor(none) ilcolor(none) color(white) lwidth(large)) xtitle("{bf:Cognitive Racial Attitudes}"  "(Awareness of Racism; Quintiles)") ytitle(`"{bf:Effect of {it:Black Name} Treatment}"') ylab(-48(12)96, nogrid) title("", size(vlarge)) legend(off) level(95) scheme(s1mono) yline(0)
graph save Graph "..\graphs\by_racial_atts_Q.gph", replace
graph combine "..\graphs\by_racial_atts_cont.gph" "..\graphs\by_racial_atts_Q.gph", ycommon scheme(s1mono) 
graph export "Figure_2.eps", as(eps) replace

***NEGATIVE BINOMIAL
nbreg sentence_scaled REF_race black REF_crime crime_2 crime_3 crime_8 crime_1 crime_7 crime_5 crime_6 REF_priors priors_2 priors_3 REF_probation probation_2 REF_age def_age_2 def_age_3 REF_job job_2, cluster(caseid)
nbreg sentence_scaled (black crime_2 crime_3 crime_8 crime_1 crime_7 crime_5 crime_6 priors_2 priors_3 probation_2 def_age_2 def_age_3 job_2)##(c.race_att c.age female c.educ c.income income_mis race_2 race_3 race_4 pid_2 pid_3), cluster(caseid)




//FEDERAL GUIDLINE/SURVEY PRESCRIPTION DESCRIPTIVES (TABLE A2)
gen cat_crime = crime_4 + crime_2*2+crime_3*3+crime_8*4+crime_1*5+crime_7*6+crime_5*7+crime_6*8
gen cat_aggravate=priors_2*2+priors_3*4+probation_2
gen full_cat=cat_aggravate+10*cat_crime
merge m:1 full_cat using "..\sentencing_guidelines.dta"
drop _merge
gen midpoint=low_bound+((high_bound-low_bound)/2) 
gen vhigh_bound=high_bound+((high_bound-low_bound)/2) 
gen vlow_bound=low_bound-((high_bound-low_bound)/2) 
gen xhigh_bound=high_bound+((high_bound-low_bound)) 
gen xlow_bound=low_bound-((high_bound-low_bound)) 
gen in_range=0
gen in_wide_range=0
gen in_xwide_range=0
replace in_range=1 if sentence_scale>=low_bound & sentence_scale<=high_bound 
replace in_wide_range=1 if sentence_scale>=vlow_bound & sentence_scale<=vhigh_bound 
replace in_xwide_range=1 if sentence_scale>=xlow_bound & sentence_scale<=xhigh_bound 
xtable cat_crime cat_aggravate, c(mean in_range mean in_wide_range mean sentence_scale median sentence_scale)


***DISTRIBUTIONS
cd ..\graphs
foreach v of varlist crime_*{
//GUIDELINES BOUNDS
qui sum low_bound if `v'==1
local lb=r(mean)
qui sum high_bound if `v'==1
local ub=r(mean)	
//TOP CODE @ 90th PCTILE
gen sentence_topcode =sentence_scaled 
qui sum sentence_scaled if `v'==1, det
local p90=r(p90)

if `ub'>`p90'{
local p90=`ub' 
}
replace sentence_topcode =`p90' if sentence_topcode>`p90'& sentence_topcode!=.

local label : variable label `v'
twoway (kdensity sentence_topcode if `v' & priors_1 & probation_1, lpattern(solid) lcolor(black)) (kdensity sentence_topcode if `v' & priors_2 & probation_1, lpattern(dash) lcolor(black)) (kdensity sentence_topcode if `v' & priors_3 & probation_1, lpattern(shortdash) lcolor(black)) (kdensity sentence_topcode if `v' & priors_1 & probation_2, lpattern(solid) lcolor(gs10)) (kdensity sentence_topcode if `v' & priors_2 & probation_2, lpattern(dash) lcolor(gs10)) (kdensity sentence_topcode if `v' & priors_3 & probation_2, lpattern(shortdash) lcolor(gs10)), graphregion(fcolor(white) lcolor(none) ilcolor(none) color(white) lwidth(large)) xtitle("Prescribed Sentence (in months)") xlabel(0(48)`p90') ytitle("Density") title("`label'") ylabel(none) legend(colfirst order(1 "No Priors" 2 "Prior Six Month Sentence" 3 "Prior Two Year Sentence") col(3) size(small) region(lcolor(white)))  xline(`lb', lcolor(black) lpattern(shortdash)) xline(`ub', lcolor(black) lpattern(shortdash))
graph save "`v'.gph", replace
drop sentence_topcode 
}
grc1leg  "crime_4.gph" "crime_2.gph" "crime_3.gph" "crime_8.gph" "crime_1.gph" "crime_7.gph" "crime_5.gph" "crime_6.gph" , scheme(s1mono) hole(9)
graph export "..\tables\Figure_A1.eps", as(eps) replace
